// pages/calendar/createSchedule/createSchedule.js
const CN_Date = require('../getCNDate.js')
import {saveschedule,editScheduleData} from "../../../api/calendar" 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    
    //备注
    getComment:"",
    //已付
    getPaied:"",
    //价格
    getPrice:"",
    //客户
    customerName:"",
    customerPhone:"",
    //地点
    location:"",
    //类型
    dateTypeIndex:1,
    dateTypeArray:['选择类型','婚礼','寿宴','宝宝宴','活动','彩排','试妆','约见','会议','约拍','其他'],
    //上午颜色
    amColor:"#D3D3D3",
    lunchColor:"#D3D3D3",
    pmColor:"#D3D3D3",
    dinnerColor:"#D3D3D3",
    nightColor:"#D3D3D3",
    //日期
    date:'',
    //农历
    lunar:"",
    //时间
    time:'',
    chinesetime:'',
    //时间段
    time_interval:'',
    //计划id
    scheduleid:"",
    //用户id
    userid:"",
    //全部
    dataList:""
  },
  //去编辑页面
  gotoedit(){
    let that = this
    wx.navigateTo({
      url: '/pages/calendar/editSchedule/editsaveSchedule/editsaveSchedule',
      events:{
        editsaveDate:function(data) {
           
        }
      },
      success:function(res) {
        res.eventChannel.emit('editsaveDate',that.data.dataList)
      }
    })
  },
  //删除计划
  delschedule() {
    let that = this;
    wx.showModal({
      title:"提示",
      content:"确定要删除吗？",
      cancelColor: 'cancelColor',
      showCancel:true,
      cancelColor:'#FF0000',
      success:function(res) {
        if(res.confirm) {
          editScheduleData({
            "schedule_id": that.data.scheduleid,
            "is_delete": "1"
          }).then((res)=>{
             
          })
          wx.navigateBack({
            delta: 1,
          })
        }
      }
    })
    // editScheduleData({
    //   "schedule_id": this.data.scheduleid,
	  //   "is_delete": "1"
    // }).then((res)=>{
    //    
    // })
  },
  //备注输入
  getCommentinput(e) {
    this.setData({
      getComment: e.detail.value
    })
  },
  //已付输入
  getPaiedinput(e) {
    this.setData({
      getPaied: e.detail.value
    })
  },
  //价格输入
  getPriceinput(e) {
    this.setData({
      getPrice: e.detail.value
    })
  },
  //电话输入
  customerPhoneinput(e) {
    this.setData({
      customerPhone:e.detail.value
    })
  },
  //名字输入
  customerNameinput(e) {
    this.setData({
      customerName: e.detail.value
    })
  },
  //地点输入
  positioninput(e) {
    this.setData({
      location: e.detail.value
    })
  },
  //保存
  savePlan() {
    if(this.data.time == "" || this.data.chinesetime == "") {
      wx.showToast({
        title: '请输入时间',
        icon:'none'
      })
    }else if(this.data.dateTypeIndex == 0) {
      wx.showToast({
        title: '请选择档期类型',
        icon:'none'
      })
    }else if(this.data.location == '') {
      wx.showToast({
        title: '请输入地址',
        icon:'none'
      })
    }else if(this.data.customerName == "") {
      wx.showToast({
        title: '请填写客户姓名',
        icon:'none'
      })
    }else if(this.data.customerPhone == "") {
      wx.showToast({
        title: '请填写客户电话',
        icon:"none"
      })
    }else if(this.data.getPrice == "") {
      wx.showToast({
        title: '请填写价格',
        icon:'none'
      })
    }else if(this.data.getPaied == '') {
      wx.showToast({
        title: '请填写已付金额',
        icon:'none'
      })
    }else if(this.data.getComment == '') {
      wx.showToast({
        title: '请填写备注',
        icon:'none'
      })
    } else {
      let time_interval = this.data.time+this.data.chinesetime;
      saveschedule({
        "date": this.data.date,
        "time_interval": time_interval,
        "type": this.data.dateTypeIndex.toString(),
        "address": this.data.location,
        "customer_name": this.data.customerName,
        "customer_phone": this.data.customerPhone,
        "price": this.data.getPrice,
        "paid": this.data.getPaied,
        "kind": "1",
        "remark": this.data.getComment
      }).then((res)=>{
         
        wx.navigateBack({
          delta: 1,
        })
      })
      
    }
    
  },
  //选择类型
  dateTypeChange(e) {
     
    this.setData({
      dateTypeIndex: e.detail.value
    })
  },
  //上午颜色
  changeAmColor(e) {
    if(this.data.amColor == '#D3D3D3') {
      this.setData({
        amColor:'#ef7995',
        lunchColor:"#D3D3D3",
        pmColor:"#D3D3D3",
        dinnerColor:"#D3D3D3",
        nightColor:"#D3D3D3",
        chinesetime:"上午"
      })
    }
  },
  changeLunchColor(e) {
    if(this.data.lunchColor == '#D3D3D3') {
      this.setData({
        lunchColor:'#ef7995',
        amColor:"#D3D3D3",
        // lunchColor:"#D3D3D3",
        pmColor:"#D3D3D3",
        dinnerColor:"#D3D3D3",
        nightColor:"#D3D3D3",
        chinesetime:"午宴"
      })
    }
  },
  changePmColor(e) {
    if(this.data.pmColor == '#D3D3D3') {
      this.setData({
        pmColor:'#ef7995',
        amColor:"#D3D3D3",
        lunchColor:"#D3D3D3",
        // pmColor:"#D3D3D3",
        dinnerColor:"#D3D3D3",
        nightColor:"#D3D3D3",
        chinesetime:"下午"
      })
    }
  },
  changeDinnerColor(e) {
    if(this.data.dinnerColor == '#D3D3D3') {
      this.setData({
        dinnerColor:'#ef7995',
        amColor:"#D3D3D3",
        lunchColor:"#D3D3D3",
        pmColor:"#D3D3D3",
        // dinnerColor:"#D3D3D3",
        nightColor:"#D3D3D3",
        chinesetime:"晚宴"
      })
    }
  },
  changeNightColor(e) {
    if(this.data.nightColor == '#D3D3D3') {
      this.setData({
        nightColor:'#ef7995',
        amColor:"#D3D3D3",
        lunchColor:"#D3D3D3",
        pmColor:"#D3D3D3",
        dinnerColor:"#D3D3D3",
        // nightColor:"#D3D3D3",
        chinesetime:"晚上"
      })
    }
  },
  //日期
  datechange(e) {
     
    ;
    let newarr = e.detail.value.split("-")
    this.setData({
      date: e.detail.value,
      lunar: CN_Date(newarr[0],newarr[1],newarr[2])
    })
  },
  //时间
  timechange(e) {
    this.setData({
      time:e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that= this;
    //获取上个页面的监听事件
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('editDate',function(data){
       
      that.setData({
        getComment:data.remark,
        getPaied:data.paid,
        getPrice:data.price,
        customerName:data.customer_name,
        customerPhone:data.customer_phone,
        location:data.address,
        dateTypeIndex:data.type,
        date:data.dateReal,
        time: data.time_interval.length > 5 ? data.time_interval.slice(0 ,5) : '',
        chinesetime: data.time_interval.slice(-2),
        scheduleid:data.schedule_id,
        userid:data.userId,
        amColor: data.amColor,
        lunchColor: data.lunchColor,
        pmColor: data.pmColor,
        dinnerColor: data.dinnerColor,
        nightColor: data.nightColor,
        dataList:data
      })
      if(data.time_interval2 == '上午') {
        that.setData({
          amColor:"#ef7995"
        })
      }
      if(data.time_interval2 == '午宴') {
        that.setData({
          lunchColor:"#ef7995"
        })
      }
      if(data.time_interval2 == '下午') {
        that.setData({
          pmColor:"#ef7995"
        })
      }
      if(data.time_interval2 == '晚宴') {
        that.setData({
          dinnerColor:"#ef7995"
        })
      }
      if(data.time_interval2 == '晚上') {
        that.setData({
          nightColor:"#ef7995"
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})